import org.cyberneko.html.parsers.SAXParser
import org.cyberneko.html.HTMLElements
import groovy.util.Node

class BabytreeController {

  def index = { }

  def static urlprefix = "http://www.babytree.com/learn"

  def zhunbeihuaiyun = {
    def host = urlprefix + "/chanqianchanhou" + "/fenmianzhishi"
    def parser = new SAXParser();
    parser.setFeature('http://xml.org/sax/features/namespaces', false)
    (1..8). each {
      def parserhost = host + "/" + it
      println parserhost
      def data = new XmlParser(parser).parse(parserhost)
      def entryUrlData = data.depthFirst().DIV.grep {it.'@class' == 'Kl_center_List'}
      entryUrlData[0].UL.LI.A.each {
        def entryhost = "http://www.babytree.com" + it.'@href'
        println entryhost
        def content = getcontent(entryhost)
        def existEntry = Entry.findBySourceUrl(entryhost)
        def mycatalog = Catalog.get(3)
        if (!existEntry) {
          def entry = new Entry()
          entry.title = it.'@title'
          entry.content = content
          entry.tagNames = "分娩知识"
          entry.sourceUrl = entryhost
          entry.catalog = mycatalog
          entry.source = "babytree"
          entry.stage = 10270
          if (entry.catalog) {
            entry.depth = entry.catalog.depth
          }
          entry.save(flush: true)
          if (!entry.hasErrors() && entry.save()) {
            render "save Ok"
          } else {
            render entry.errors
          }
        } else {
          existEntry.title = it.'@title'
          existEntry.content = content
          existEntry.save(flush: true)
          render 'updated'
        }
      }
    }
  }

  def getcontent(String url) {
    println("***" + url)
    def parser = new SAXParser()
    parser.setFeature('http://xml.org/sax/features/namespaces', false)
    def data = new XmlParser(parser).parse(url)
    def entrydata = data.depthFirst().DIV.grep {it.'@class' == 'K_content'}
    def contentBuffer = new StringBuffer()
    def size = entrydata[0].children().size()
    println "********" + size
    for (int i = 0; i < size; i++) {
      //entrydata[0].children.each {
      def entryList = entrydata[0].children()
      //println entryList[i]
      def it = entryList[i]
      if (it instanceof String) {
        contentBuffer << it
      } else if (it.name().toString().indexOf("DIV") != -1) {
        if(it.'@class'!= 'timeKeyword' && it.'@class'!= 'otherAsk' && it.'@class' != 'digDiv' && it.'@class' != 'editor'){
          contentBuffer << divcontent(it)
        }
      } else if (it.name().toString().indexOf("SPAN") != -1) {
        contentBuffer << spancontent(it)
      } else if (it.name().toString().indexOf("P") != -1) {
        contentBuffer << pcontent(it)
      } else if (it.name().toString().indexOf("FONT") != -1) {
        contentBuffer << fontcontent(it)
      } else if (it.name().toString().indexOf("IMG") != -1) {
        contentBuffer << imgcontent(it)
      } else if (it.name().toString().indexOf("A")!=-1){
        contentBuffer << it.text()
      } else if (it.name().toString().indexOf("TABLE")!=-1){
        println "in table"
        contentBuffer << tablecontent(it)
      } else if (it.name().toString().indexOf("H3") != -1){
        contentBuffer << h3content(it)
      }
    }
    println("content::::::: " + contentBuffer.toString())
    return contentBuffer.toString()
  }

  def h3content(Node h3){
    def h3Buffer = new StringBuffer()
    def h3List = h3.children()
    def size = h3List.size()
    h3Buffer.append("<H3>")
    for(int i = 0;i<size; i++){
      def child = h3List[i]
      if(child instanceof String){
        h3Buffer << child
      } else if(child.name().toString().indexOf("P")!= -1){
        h3Buffer << pcontent(child)
      } else if(child.name().toString().indexOf("FONT")!= -1){
        h3Buffer << fontcontent(child)
      } else if(child.name().toString().indexOf("SPAN")!=-1){
        h3Buffer << spancontent(child)
      }
    }
    h3Buffer << "</H3>"
    return h3Buffer.toString()
  }

  def tablecontent(Node table){
    def tableBuffer = new StringBuffer()
    println table
    def td = table.tbody.tr[0].td[0]
    def childList = td.children()
    def size = child.size()
    for(int i = 0; i<size; i++){
      def child = td[i]
      if(child instanceof String){
        tableBuffer << child
      }else if (child.name().toString().indexOf("P") != -1) {
        tableBuffer << pcontent(it)
      } else if (child.name().toString().indexOf("FONT") != -1) {
        tableBuffer << fontcontent(it)
      } else if (child.name().toString().indexOf("IMG") != -1) {
        tableBuffer << imgcontent(it)
      } else if (child.name().toString().indexOf("A")!=-1){
        tableBuffer << it.text()
      }
      return tableBuffer.toString()
    }
  }
  def getsubcontent(Node element) {
    println "in getsubcontent"
    println element
    println "ends"
  }

  def imgcontent(Node img) {
    StringBuffer imgBuffer = new StringBuffer()
    imgBuffer << "<img src='" << img.'@src' << "'>"
    return imgBuffer.toString()
  }

  def fontcontent(Node font) {
    StringBuffer fontBuffer = new StringBuffer()
    if (!(font.'@face' == null || font.'@face'.toString().equals(""))) {
      fontBuffer << "<font face='" << font.'@face' << "'>"
    } else {
      fontBuffer << "<font>"
    }
    def childList = font.children()
    def size = childList.size()
    for (int i = 0; i < size; i++) {
      def child = childList[i]
      if (child instanceof String) {
        fontBuffer << child
      } else if (child.name().toString().indexOf("IMG") != -1) {

      } else if (child.name().toString().indexOf("BR") != -1) {
        fontBuffer << "<br/>"
      } else if (child.name().toString().indexOf("SPAN") != -1) {
        fontBuffer << spancontent(child)
      } else if(child.name().toString().indexOf("FONT")!=-1){
        fontBuffer << fontcontent(child)       
      } else if(child.name().toString().indexOf("STRONG")!=-1){
        fontBuffer << "<strong>" << child.text() << "</strong>"
      } else if(child.name().toString().indexOf("P")!=-1){
        fontBuffer << pcontent(child)
      } else if (child.name().toString().indexOf("H3") != -1){
        fontBuffer << h3content(child)
      }else {
        // ignore others
      }
    }
    fontBuffer << "</font>"
    return fontBuffer.toString()
  }

  def spancontent(Node span) {
    StringBuffer spanBuffer = new StringBuffer()
    spanBuffer << "<span>"
    def spanList = span.children()
    def size = spanList.size()
    for (int i = 0; i < size; i++) {
      def child = spanList[i]
      if (child instanceof String) {
        spanBuffer << child
      } else if (child.name().indexOf("BR") != -1) {
        spanBuffer << "<br/>"
      } else if (child.name().indexOf("FONT") != -1) {
        spanBuffer << fontcontent(child)
      } else if(child.name().toString().indexOf("STRONG")!=-1){
        spanBuffer << "<strong>" << child.text() << "</strong>"
      }else if(child.name().toString().indexOf("P")!=-1){
        spanBuffer << pcontent(child)
      } else if (child.name().toString().indexOf("H3") != -1){
        spanBuffer << h3content(child)
      }else if (child.name().toString().indexOf("SPAN") != -1){
        spanBuffer << spancontent(child)
      }else {
        // ignore others
      }
    }
    spanBuffer << "</span>"
    return spanBuffer.toString()
  }

  def pcontent(Node p) {
    StringBuffer pBuffer = new StringBuffer()
    pBuffer << "<p>"
    def pList = p.children()
    def size = pList.size()
    for (int i = 0; i < size; i++) {
      def child = pList[i]
      if (child instanceof String) {
        pBuffer << child
      } else if (child.name().indexOf("BR") != -1) {
        pBuffer << "<br/>"
      } else if (child.name().indexOf("FONT") != -1) {
        pBuffer << fontcontent(child)
      } else if (child.name().indexOf("SPAN") != -1) {
        pBuffer << spancontent(child)
      } else if (child.name().indexOf("IMG") != -1) {
        pBuffer << imgcontent(child)
      } else if (child.name().toString().indexOf("H3") != -1){
        pBuffer << h3content(child)
      }
    }
    pBuffer << "</p>"
    return pBuffer.toString()
  }

  def divcontent(Node div) {
    StringBuffer divBuffer = new StringBuffer()
    divBuffer << "<div>"
    def divList = div.children()
    def size = divList.size()
    for (int i = 0; i < size; i++) {
      def child = divList[i]
      if (child instanceof String) {
        divBuffer << child
      } else if (child.name().indexOf("P") != -1) {
        divBuffer << pcontent(child)
      } else if (child.name().indexOf("SPAN") != -1) {
        divBuffer << spancontent(child)
      } else if (child.name().indexOf("font") != -1) {
        divBuffer << fontcontent(child)
      } else if (child.name().indexOf("IMG") != -1) {
        divBuffer << imgcontent(child)
      } else if (child.name().indexOf("BR") != -1) {
        divBuffer << "<BR/>"
      } else {
        //ignore
      }
    }
    divBuffer << "</div>"
    return divBuffer.toString()
  }


  def test = {
    def url = "http://www.babytree.com/learn/article/15262"
    def content = getcontent(url)
    println content
    render 'test'
  }

}
